package com.pox.guli.service.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pox.guli.common.base.result.R;
import com.pox.guli.service.edu.entity.Course;
import com.pox.guli.service.edu.entity.Teacher;
import com.pox.guli.service.edu.entity.vo.TeacherQueryVo;
import com.pox.guli.service.edu.feign.OssFeignService;
import com.pox.guli.service.edu.mapper.CourseMapper;
import com.pox.guli.service.edu.mapper.TeacherMapper;
import com.pox.guli.service.edu.service.TeacherService;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * <p>
 * 讲师 服务实现类
 * </p>
 *
 * @author pox21s
 * @since 2022-06-01
 */
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService {

    @Resource
    private OssFeignService ossFeignService;
    @Resource
    private CourseMapper courseMapper;

    /**
     * 分页查询、搜索查询
     *
     * @return com.baomidou.mybatisplus.core.metadata.IPage<com.pox.guli.service.edu.entity.Teacher>
     */
    @Override
    public IPage<Teacher> selectPage(Long page, Long limit, TeacherQueryVo teacherQueryVo) {
        /* 分页查询 */
        Page<Teacher> pageParam = new Page<>(page, limit);
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();

        queryWrapper.orderByAsc("sort");

        /* 组装特定查询 */
        if (teacherQueryVo == null) {
            return baseMapper.selectPage(pageParam, queryWrapper);
        }

        // 如果传入了具体条件，那么进行具体的条件查询
        String name = teacherQueryVo.getName();
        Integer level = teacherQueryVo.getLevel();
        String joinDateBegin = teacherQueryVo.getJoinDateBegin();
        String joinDateEnd = teacherQueryVo.getJoinDateEnd();

        // 判断条件的可用性并组装到条件查询中
        if (!StringUtils.isEmpty(name)) {
            queryWrapper.likeRight("name", name);
        }
        if(level != null) {
            queryWrapper.eq("level", level);
        }
        if (!StringUtils.isEmpty(joinDateBegin)) {
            queryWrapper.ge("join_date", joinDateBegin);
        }
        if (!StringUtils.isEmpty(joinDateEnd)) {
            queryWrapper.le("join_date", joinDateEnd);
        }

        // 根据拼装的查询条件返回结果
        return baseMapper.selectPage(pageParam, queryWrapper);
    }

    /**
     * 根据左关键字查询
     * @param key 关键字
     * @return java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
     */
    @Override
    public List<Map<String, Object>> selectNameListByKey(String key) {
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("name");
        queryWrapper.likeRight("name", key);

        List<Map<String, Object>> list = baseMapper.selectMaps(queryWrapper);//返回值是Map列表
        return list;
    }

    @Override
    public boolean removeAvatarById(String id) {
        Teacher teacher = baseMapper.selectById(id);

        if (teacher != null) {
            String avatar = teacher.getAvatar();
            if (!StringUtils.isEmpty(avatar)) {
                //删除图片
                R r = ossFeignService.removeFile(avatar);

                return r.getSuccess();
            }
        }
        return false;
    }

    @Override
    public Map<String, Object> selectTeacherInfoById(String id) {
        // 获取讲师基本信息
        Teacher teacher = baseMapper.selectById(id);

        // 获取讲师课程信息
        QueryWrapper<Course> courseQueryWrapper = new QueryWrapper<>();
        List<Course> courseList = courseMapper.selectList(courseQueryWrapper);

        // 封装数据返回
        Map<String, Object> map = new ConcurrentHashMap<>(2);
        map.put("teacher", teacher);
        map.put("courseList", courseList);

        return map;
    }

    @Cacheable(value = "index", key = "'selectHotTeacher'")
    @Override
    public List<Teacher> selectHotTeacher() {

        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("sort");
        // 设置返回的个数
        queryWrapper.last("limit 4");

        return baseMapper.selectList(queryWrapper);
    }
}
